package tech.yunland.shop.domain.service;

import com.alibaba.fastjson.JSON;
import com.moyuer.bsf.domain.model.User;
import com.moyuer.bsf.domain.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tech.yunland.shop.domain.dao.SchedulerJobDao;
import tech.yunland.shop.domain.model.SchedulerJob;
import tech.yunland.shop.domain.task.IDoSchedulerJob;

import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 任务执行实现类
 *
 * @author 文卡<wkwenka@gmail.com>  on 17-3-13.
 */
@Service
public class DoSchedulerJob implements IDoSchedulerJob {

    @Autowired
    private UserService userService;

    @Autowired
    private SchedulerJobDao schedulerJobDao;

    @Autowired
    private UserActivityService userActivityService;

    @Value("${active_score}")
    private String activeScore;

    @Override
    public void run(SchedulerJob schedulerJob) {

        System.out.println(JSON.toJSONString(schedulerJob, true));

        SchedulerJob _schedulerJob = schedulerJobDao.get(schedulerJob.getId());

        //任务状态设为禁用
        if (_schedulerJob != null) {
            if (StringUtils.isBlank(_schedulerJob.getRemark())){

                User creator = _schedulerJob.getCreator();

                //冻结账号
                creator.setState(Integer.valueOf(0));

                userService.saveOrUpdate(creator);
                _schedulerJob.setState(Integer.valueOf(0));
                schedulerJobDao.update(_schedulerJob);
            }
            //该定时任务是计算活跃度
            else if (StringUtils.isNotBlank(_schedulerJob.getRemark()) && _schedulerJob.getRemark().contains("活跃度")){
                List<Map<String, Object>> maps = new LinkedList<>();
                maps = this.getSaleBillSum(maps);
                maps = this.getStoreBillSum(maps);
                maps = this.getPurchaseBillSum(maps);

                for (Map<String, Object> map : maps) {
                    BigInteger count = (BigInteger) map.get("sum");
                    String userId = (String) map.get("creator_id");
                    //修改用户活跃度
                    userActivityService.saveOrUpdate(userId, count.multiply(BigInteger.valueOf(Long.valueOf(activeScore))));
                }
            }
        }

    }


    /**
     * 获取当天内销售单制单数
     *
     * @param list
     * @return
     */
    private List<Map<String, Object>> getSaleBillSum(List<Map<String, Object>> list) {
        String sql = "SELECT\n" +
                "  count(bill_sale.sn) AS sum,\n" +
                "  bill_sale.creator_id\n" +
                "FROM bill_sale\n" +
                "WHERE\n" +
                "  date(bill_sale.create_time) = current_date\n" +
                "GROUP BY bill_sale.creator_id";
        List<Map<String, Object>> bySQL = schedulerJobDao.findBySQL(sql);
        list.addAll(bySQL);
        return list;
    }

    /**
     * 获取当天内出入库制单数
     *
     * @param list
     * @return
     */
    private List<Map<String, Object>> getStoreBillSum(List<Map<String, Object>> list) {
        String sql = "SELECT\n" +
                "  count(bill_store.sn) AS sum,\n" +
                "  bill_store.creator_id\n" +
                "FROM bill_store\n" +
                "WHERE\n" +
                "  date(bill_store.create_time) = current_date\n" +
                "GROUP BY bill_store.creator_id";
        List<Map<String, Object>> bySQL = schedulerJobDao.findBySQL(sql);
        list.addAll(bySQL);
        return list;
    }

    /**
     * 获取当天内采购单制单数
     *
     * @param list
     * @return
     */
    private List<Map<String, Object>> getPurchaseBillSum(List<Map<String, Object>> list) {
        String sql = "SELECT\n" +
                "  count(purchase_bill.sn) AS sum,\n" +
                "  purchase_bill.creator_id\n" +
                "FROM purchase_bill\n" +
                "WHERE\n" +
                "  date(purchase_bill.create_time) = current_date\n" +
                "GROUP BY purchase_bill.creator_id";
        List<Map<String, Object>> bySQL = schedulerJobDao.findBySQL(sql);
        list.addAll(bySQL);
        return list;
    }

}
